package com.gxitsky.service.impl;

import com.gxitsky.exception.BusinessException;
import com.gxitsky.service.WebSiteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gxing
 * @desc 百度站长资料
 * @date 2022/12/5
 */
@Slf4j
@Service
public class WebSiteServiceImpl implements WebSiteService {
    private static String BAIDU_COMMIT_URL = "http://data.zz.baidu.com/urls?site=www.gxitsky.com&token=fkyfjoLoVYY1VGtS";
    private static String DEFAULT_WEB_SITE_URL = "http://www.gxitsky.com";
    private static String DEFAULT_WEB_SITE_DOMAIN = "www.gxitsky.com";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @desc 推送站点资源到百度搜索
     * @author gxing
     * @date 2022/12/5
     */
    @Override
    public void pushWebSiteResource() {
        try {
            long start = System.currentTimeMillis();
            Document doc = Jsoup.connect(DEFAULT_WEB_SITE_URL).get();
            Elements link = doc.select("a");
            List<String> absHrefList = link.eachAttr("abs:href");
            List<String> urlList1 = absHrefList.stream().filter(e -> e.contains(DEFAULT_WEB_SITE_DOMAIN)).distinct().collect(Collectors.toList());

            List<String> urlList2 = new ArrayList<>(urlList1);
            urlList1.parallelStream().forEach(e -> {
                List<String> urls = getUrls(e);
                urlList2.addAll(urls);
            });
            List<String> urlList3 = urlList2.stream().distinct().collect(Collectors.toList());
            log.info("----->url number:{}", urlList3.size());
            log.info("----->parse web url consume time:{}s", (System.currentTimeMillis() - start) / 1000);

            int batchSize = 1000;
            List<List<String>> list = ListUtils.partition(urlList3, batchSize);
            list.parallelStream().forEach(e -> {
                StringBuilder sb = new StringBuilder();
                e.forEach(url -> sb.append(url).append("\n"));
                String result = postRequest(sb.toString());
                log.info("----->推送结果:{}", result);
            });
            log.info("----->push web url consume time:{}s", (System.currentTimeMillis() - start) / 1000);
        } catch (IOException e) {
            throw new BusinessException(e);
        }
    }

    public List<String> getUrls(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements link = doc.select("a");
            List<String> absHrefList = link.eachAttr("abs:href");
            return absHrefList.stream().filter(e -> e.contains(DEFAULT_WEB_SITE_DOMAIN)).distinct().collect(Collectors.toList());
        } catch (IOException e) {
            log.error("----->发送 POST 请求出现异常:", e);
        }
        return Collections.emptyList();
    }

    public String postRequest(String body) {
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType(MediaType.TEXT_PLAIN_VALUE);
        headers.setContentType(mediaType);
        HttpEntity<String> formEntity = new HttpEntity<>(body, headers);
        String result = restTemplate.postForObject(BAIDU_COMMIT_URL, formEntity, String.class);
        log.info("----->请求结果:{}", result);
        return result;
    }
}
